import { OrdersCards } from './ordersCard';
let _OrdersCards = OrdersCards;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import OrdersCardMock from './mallm/orders/ordersCardMock';
 * OrdersCardMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/orders/ordersCard/listKey').reply(config => {
      let params = config.params;
      let mockOrdersCards = _OrdersCards.filter(ordersCard => {
      	if(params.userCardCode||params.userCardCode==''){ if( ordersCard.userCardCode.indexOf(params.userCardCode)>=0) return true;}
      	if(params.orderId||params.orderId==''){ if( ordersCard.orderId.indexOf(params.orderId)>=0) return true;}
      	if(params.reduceMoney||params.reduceMoney==''){ if( ordersCard.reduceMoney.indexOf(params.reduceMoney)>=0) return true;}
      	if(params.increaseBonus||params.increaseBonus==''){ if( ordersCard.increaseBonus.indexOf(params.increaseBonus)>=0) return true;}
      	if(params.reduceBonus||params.reduceBonus==''){ if( ordersCard.reduceBonus.indexOf(params.reduceBonus)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( ordersCard.deptid.indexOf(params.deptid)>=0) return true;}
      	if(params.id||params.id==''){ if( ordersCard.id.indexOf(params.id)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockOrdersCards
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/orders/ordersCard/list').reply(config => {
      let params = config.params;
      let mockOrdersCards = _OrdersCards.filter(ordersCard => {
      	var isTrue=false;
      	if(params.userCardCode||params.userCardCode==''){if(ordersCard.userCardCode.indexOf(params.userCardCode)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.orderId||params.orderId==''){if(ordersCard.orderId.indexOf(params.orderId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.reduceMoney||params.reduceMoney==''){if(ordersCard.reduceMoney.indexOf(params.reduceMoney)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.increaseBonus||params.increaseBonus==''){if(ordersCard.increaseBonus.indexOf(params.increaseBonus)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.reduceBonus||params.reduceBonus==''){if(ordersCard.reduceBonus.indexOf(params.reduceBonus)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(ordersCard.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.id||params.id==''){if(ordersCard.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockOrdersCards
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockOrdersCards.length;
      mockOrdersCards = mockOrdersCards.filter((ordersCard, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockOrdersCards,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/orders/ordersCard/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _OrdersCards = _OrdersCards.filter(ordersCard => ordersCard.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/orders/ordersCard/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _OrdersCards = _OrdersCards.filter(ordersCard => !params.includes(ordersCard.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/orders/ordersCard/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { userCardCode,orderId,reduceMoney,increaseBonus,reduceBonus,deptid,id } = params;
      _OrdersCards.some(ordersCard => {
        if (ordersCard.id === id) {
			ordersCard.userCardCode = userCardCode;
			ordersCard.orderId = orderId;
			ordersCard.reduceMoney = reduceMoney;
			ordersCard.increaseBonus = increaseBonus;
			ordersCard.reduceBonus = reduceBonus;
			ordersCard.deptid = deptid;
			ordersCard.id = id
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/orders/ordersCard/add').reply(config => {
      var params = JSON.parse( config.data );
      let { userCardCode,orderId,reduceMoney,increaseBonus,reduceBonus,deptid,id } = params;
      _OrdersCards.push({
		userCardCode:userCardCode,
		orderId:orderId,
		reduceMoney:reduceMoney,
		increaseBonus:increaseBonus,
		reduceBonus:reduceBonus,
		deptid:deptid,
		id:id
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};